
class RequestOperation():

    def __init__(self):
        self.flag = True

    # 实现请求发送，在实际接口开发过程中其响应的数据格式一般都市json
    def send_request(self, obj, method, url, data, cookies=None, headers=None):
        # get_response = requests.request(method, url=url, data=data)
        if method == "GET" or method == "get" or method == "Get":
            return obj.get(url=url, params=data, cookies=cookies, headers=headers)
        elif method == "POST" or method == "Post" or method == "post":
            return obj.post(url=url, json=data, cookies=cookies, headers=headers)
        else:
            return "请求方法错误"

    def get_response(self, obj, method, url, data, cookies=None, headers=None):
        __get_rep = self.send_request(obj, method, url, data, cookies, headers=headers)
        if __get_rep == "请求方法错误":
            pass
        else:
           # 如果获取json格式抛弃异常说明当前相应非json格式既可以取获取其文本
           try:
               self.flag = True
               return __get_rep.json()
           except:
               self.flag = False
               # 这些都是返回的内容都不是json格式的情况下
               response_dict= {}

               # 请求体中文本的长度
               response_dict["Content-Length"] = __get_rep.headers['Content-Length']

               # 文本类型
               response_dict["Content-Type"] = __get_rep.headers['Content-Type']

               # 然后返回文本内容
               response_dict["text"] = __get_rep.text
               return response_dict

